package com.tuananh.radnom.service.config.impl;

import java.util.ArrayList;

import com.tuananh.radnom.model.NetInterface;
import com.tuananh.radnom.model.NetNode;
import com.tuananh.radnom.model.iputils.IPRange;
import com.tuananh.radnom.model.utils.IPUtils;
import com.tuananh.radnom.persist.imp.xml.NetIntXmlDAO;
import com.tuananh.radnom.service.config.INetIntService;

public class NetIntService implements INetIntService {
	NetIntXmlDAO netIntDAO;
	public NetIntXmlDAO getNetIntDAO() {
		return netIntDAO;
	}

	public void setNetIntDAO(NetIntXmlDAO netIntDAO) {
		this.netIntDAO = netIntDAO;
	}

	public PollService getPollService() {
		return pollService;
	}

	public void setPollService(PollService pollService) {
		this.pollService = pollService;
	}
	PollService pollService;
	public boolean persit() {
		// TODO Auto-generated method stub
		return netIntDAO.persit();
	}

	public boolean add(NetInterface netInt, String nodeName) {
		// TODO Auto-generated method stub
		return netIntDAO.add(netInt, nodeName);
	}

	public boolean update(NetInterface netInt) {
		// TODO Auto-generated method stub
		return netIntDAO.update(netInt);
	}

	public boolean remove(String name) {
		// TODO Auto-generated method stub
		if (netIntDAO.remove(name)){
			//pollService.removePollInfo(null, name);
			return true;
		}
		return false;
	}

	public ArrayList<NetInterface> getAll() {
		// TODO Auto-generated method stub
		return netIntDAO.getAll();
	}

	public NetInterface getByName(String name) {
		// TODO Auto-generated method stub
		return netIntDAO.getByName(name);
	}
	
	public ArrayList<NetInterface> getInRange(IPRange range){
		ArrayList<NetInterface> listNetInt = new ArrayList<NetInterface>();
		ArrayList<NetInterface> source = netIntDAO.getAll();
		for(NetInterface netInt:source){
			if (IPUtils.inRange(range, netInt.getIpAddress()))
			{
				listNetInt.add(netInt);
			}
		}
		return listNetInt;
	}
	public String getLastedGenerateNetIntName(){
		ArrayList<NetInterface> listNetInt = netIntDAO.getAll();
		int maxNumber = 0;
		for(NetInterface netInt:listNetInt){
			if (netInt.getName().startsWith("AutoGenerateNetInt_")){
				int number =  Integer.parseInt(netInt.getName().substring(19));
				if (number>maxNumber) maxNumber=number;
			}
		}
		return "AutoGenerateNetInt_"+(maxNumber+1);
	}
	public NetInterface getByIp(String ip) {
		// TODO Auto-generated method stub
		return netIntDAO.getByIp(ip);
	}

	public boolean merge(NetInterface first, NetInterface second) {
		// TODO Auto-generated method stub
		return netIntDAO.merge(first, second);
	}
}
